package easy;

import java.util.HashSet;

/**
 * @author admin
 * 771. 宝石与石头
 * 解法一：暴力解法(省空间)
 * 解法二：hash解法，用一个hashset来保存宝石，对石头进行遍历，若hashset里包含该字母，则将记录宝石数的count+1，最后返回count
 */
public class LeeCode771 {

//    public int numJewelsInStones(String jewels, String stones) {
//
//        int count = 0;
//        for (int i = 0; i < jewels.length(); i++) {
//            char c = jewels.charAt(i);
//            for (int j = 0; j < stones.length(); j++) {
//                char s = stones.charAt(j);
//                if (c == s){
//                    count++;
//                }
//            }
//        }
//        return count;
//
//    }
    
    public int numJewelsInStones(String jewels, String stones){
        
        int count = 0;
        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            char c = jewels.charAt(i);
            if (!set.contains(c)){
                set.add(c);
            }
        }

        for (int i = 0; i < stones.length(); i++) {
            char c = stones.charAt(i);
            if (set.contains(c)){
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(new LeeCode771().numJewelsInStones("aA", "aAAbbbb"));
    }
}
